<?php
class book_model extends MY_Model
{
	public
		$table = 'literature'
		//$prKey = 'users_id',
		//$publishedField = 'faq_status',

	;

	function __construct()
	{
		parent::__construct();


	}


    function search($search_text) {
 		$userID=$this->auth->getID();
    	$sql = "SELECT *
    			FROM literature
    			WHERE name LIKE '".$search_text."' OR author LIKE '".$search_text."' OR about LIKE '".$search_text."'";
        $result = $this->db->query($sql)->row_array();
        return $result;
    }
    



	function search_books_hom()
	{	
		$this->db->from($this->table);
		$this->db->order_by("lit_id", "desc");
		$this->db->limit(10);
		$this->data['book_list'] = $this->db->get()->result();
		
	}
	
	function searchBook($params) {
		
		if(!empty($params['query'])) {
			//$query = trim($params['query']);
			$query = explode(' ', $params['query']);
			//print_r($query);
			$s_fields = array('lit_author', 'lit_name', 'lit_year');
			$sql = "SELECT DISTINCT * FROM ".$this->table." WHERE ";
			$orsql = '';
			$first = true;
			$empty = true;
			foreach($query as $key => $value) {
				//$value = $this->db->escape($value);
				//$sql .= "lit_author LIKE '%".$value ."%'";// OR lit_name LIKE '%".$value."%' OR lit_year=". intval($value)." OR lit_id IN(SELECT literature_id FROM literature_sections where sections_id IN(SELECT section_id FROM sections WHERE section_name LIKE '%".$value."%'))";
				$orsql = '';
				foreach($s_fields as $field) {
					
					if($field == 'lit_year') {
						if(is_numeric($value) && !$params['for_name']) {
							$orsql .= " ".$field."=".$value;// OR lit_name LIKE '%".$value."%' OR lit_year=". intval($value)." OR lit_id IN(SELECT literature_id FROM literature_sections where sections_id IN(SELECT section_id FROM sections WHERE section_name LIKE '%".$value."%'))";
							$empty = false;
						}
					} else {
						$orsql .= " ".$field." LIKE '%".$value ."%'";// OR lit_name LIKE '%".$value."%' OR lit_year=". intval($value)." OR lit_id IN(SELECT literature_id FROM literature_sections where sections_id IN(SELECT section_id FROM sections WHERE section_name LIKE '%".$value."%'))";
						$empty = false;
					}
					if($params['lang_only'] && !$empty) {
						$orsql .= " AND lit_lang='".$params['lang_only']."'";
					}
					if(!$first && !$empty) {
						$orsql = " OR" . $orsql;
					}
					$sql .= $orsql;
					$orsql = '';
					$first = false;
					$empty = true;
				}
				if(!$params['for_name']) {
					$sql .= " OR lit_id IN(SELECT literature_id FROM literature_sections where sections_id IN(SELECT section_id FROM sections WHERE section_name LIKE '%".$value."%'))";
					if($params['lang_only']) {
						$sql .= " AND lit_lang='".$params['lang_only']."'";
					}
				}
			}
			//if($params['ru_only']) {
			//	$sql .= " AND lit_lang='ru'";
			//}
			//echo $sql;
			return $this->db->query($sql)->result();
		} else {
			return null;
		}
	}
	
	function updateRating($id, $mark) {
		$this->db->query('UPDATE '.$this->table.' SET lit_mark=(lit_mark*lit_voters+'.$mark.')/(lit_voters+1), lit_voters=lit_voters+1'.' WHERE lit_id='.$id);
		$this->db->from($this->table);
		$this->db->select('lit_mark');
		$this->db->where('lit_id', $id);
		$result = $this->db->get()->row_array();
		return round($result['lit_mark']);
	}
}
?>